package phone.example.com.a36kr.other;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import phone.example.com.a36kr.R;
import phone.example.com.a36kr.activity.NewsSecondActivity;
import phone.example.com.a36kr.adapter.PopWindowLvAdapter;
import phone.example.com.a36kr.bean.PopWindowBean;
import phone.example.com.a36kr.nettool.NetTool;
import phone.example.com.a36kr.tools.MyApp;


/**
 * Created by dllo on 16/8/23.
 */
public class SecondPopupWindow extends PopupWindow {

    private String urlFront = "https://rong.36kr.com/api/mobi/news/";
    private String urlRear = "/author-region";
    private String urlAdd;
    private final ListView lv;
    private final PopWindowLvAdapter popWinAdapter;
    private final TextView tvLeft;
    private final TextView tvRight;
    private Context context;



    public void setUrlAdd(String urlAdd) {
        this.urlAdd = urlAdd;
        initNet();

    }

    public SecondPopupWindow(final Context context, View.OnClickListener itemsOnClick) {
        super(context);
        this.context = context;

        View view = LayoutInflater.from(context).inflate(R.layout.news_second_pop_window, null);//绑定布局
        lv = (ListView) view.findViewById(R.id.second_pop_windows_lv);
        popWinAdapter = new PopWindowLvAdapter(context);
        tvLeft = (TextView) view.findViewById(R.id.tv_window_second_totalCount);
        tvRight = (TextView) view.findViewById(R.id.tv_window_second_totalView);


        this.setContentView(view);
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0xb0000000);//设置窗口的颜色
        this.setBackgroundDrawable(dw);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                dismiss();
                return false;
            }
        });
    }


    private void initNet() {
        NetTool mNetTool = new NetTool();

        Log.e("SecondPopupWindow", (urlAdd + urlRear));

        mNetTool.getNetData(urlAdd + urlRear, PopWindowBean.class, new NetTool.NetListener<PopWindowBean>() {

            @Override
            public void onSuccess(PopWindowBean popWindowBean) {

                tvLeft.setText(popWindowBean.getData().getTotalCount() + "篇");
                tvRight.setText(popWindowBean.getData().getTotalView()/10000 + "万");
                popWinAdapter.setPopWindowBeen(popWindowBean);
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(context, NewsSecondActivity.class);
                        // valueOf 转码
                        String postId = String.valueOf(popWinAdapter.getFeedId());
                        intent.putExtra("newsFeedId",postId);
                        context.startActivity(intent);
                    }
                });

                lv.setAdapter(popWinAdapter);
            }

            @Override
            public void onError(String errorMsg) {
                Toast.makeText(MyApp.getContext(), "数据请求失败,请检查网络连接", Toast.LENGTH_SHORT).show();
            }
        });



    }


}






